package com.example.biye;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.biye.permission.CustomPermissionUtil;
import com.example.biye.permission.PermissionCallback;
import com.example.biye.permission.RequestBean;

public class TabThirdActivity extends AppCompatActivity {

    private String TAG;
    String tag = "----" + TabThirdActivity.class.getSimpleName();
    static Context mAppContext;
    //要申请的权限
    final String[] permission = new String[]{Manifest.permission.SEND_SMS,
            };


    //要申请权限的回调
    PermissionCallback callback = new PermissionCallback() {

        @Override
        public void onPermissionGranted() {
            Toast.makeText(TabThirdActivity.this, "权限申请成功--方式2", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(TabThirdActivity.this, JinjiActivity.class);
            startActivity(intent);
        }


        @Override
        public void shouldShowRational(final RequestBean requestBean, String[] rationalPermissons, final boolean before) {

            StringBuilder sb = new StringBuilder();
            sb.append("我们将获取以下权限:\n\n");

            for (int i = 0; i < rationalPermissons.length; i++) {
                sb.append((i + 1) + "、");
                /*if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(rationalPermissons[i])) {
                    sb.append("读写设备外部存储空间的权限，将被用于获取用户头像、保存一些文件到项目文件夹中");
                } else if (Manifest.permission.CAMERA.equals(rationalPermissons[i])) {
                    sb.append("使用摄像头的权限将，被用于拍照获取用户头像，直播视频采集");
                } else if (Manifest.permission.READ_PHONE_STATE.equals(rationalPermissons[i])) {
                    sb.append("读取手机状态信息的权限，将被用于登录时账号验证");
                } else if (Manifest.permission.RECORD_AUDIO.equals(rationalPermissons[i])) {
                    sb.append("录制引起的权限，将被用于直播音频采集");
                }*/
                if (Manifest.permission.SEND_SMS.equals(rationalPermissons[i])) {
                    sb.append("将要获取SEND_SMS");
                }
                if (i == rationalPermissons.length - 1) {
                    sb.append("。");
                } else {
                    sb.append("；");
                }

                sb.append("\n");
            }

            CustomPermissionUtil.showDialog(TabThirdActivity.this, sb.toString(),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //重新申请权限
                            CustomPermissionUtil.requestAgain(TabThirdActivity.this, requestBean);
                        }
                    });
        }



        @Override
        public void onPermissonReject(String[] rejectPermissons) {
            StringBuilder sb = new StringBuilder();
            sb.append("我们需要的权限:\n\n");

            for (int i = 0; i < rejectPermissons.length; i++) {
                sb.append((i + 1) + "、");
                /*if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(rejectPermissons[i])) {
                    sb.append("读写外部存储空间的权限，将被用于获取用户头像、保存一些文件到项目文件夹中");
                } else if (Manifest.permission.CAMERA.equals(rejectPermissons[i])) {
                    sb.append("使用摄像头的权限，被用于拍照获取用户头像，直播视频采集");
                } else if (Manifest.permission.READ_PHONE_STATE.equals(rejectPermissons[i])) {
                    sb.append("读取手机状态信息的权限，将被用于登录时账号验证");
                } else if (Manifest.permission.RECORD_AUDIO.equals(rejectPermissons[i])) {
                    sb.append("录制引起的权限，将被用于直播音频采集");
                }*/
                if (Manifest.permission.SEND_SMS.equals(rejectPermissons[i])) {
                    sb.append("将要获取SEND_SMS");
                }
                if (i == rejectPermissons.length - 1) {
                    sb.append("。");
                } else {
                    sb.append("；");
                }
                sb.append("\n");
            }
            sb.append("\n被设为禁止,请到设置里开启权限");

            CustomPermissionUtil.showDialog(TabThirdActivity.this, sb.toString(),
                    getString(R.string.dialog_cancel),
                    getString(R.string.dialog_go_setting),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //重新申请权限
                            CustomPermissionUtil.startSettingsActivity(TabThirdActivity.this);
                        }
                    }
            );
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_third);
        Button buttontc = (Button) findViewById(R.id.buttontc);
        Button buttonziliao = (Button) findViewById(R.id.buttonziliao);
        Button buttonwomen = (Button) findViewById(R.id.buttonwomen);
        Button buttonlianxi = (Button) findViewById(R.id.buttonlianxi);
        Button buttonjilu = (Button) findViewById(R.id.buttonjilu);
        buttontc.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TabThirdActivity.this, MainActivity.class);
                        startActivity(intent);

                    }
    });
        buttonziliao.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TabThirdActivity.this, GerenziliaoActivity.class);
                        startActivity(intent);
                    }

});

        buttonwomen.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TabThirdActivity.this, WoActivity.class);
                        startActivity(intent);
                    }

                });
        buttonlianxi.setOnClickListener((v) -> {
            CustomPermissionUtil.request(getApplicationContext(), permission, callback);
        });
        buttonjilu.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TabThirdActivity.this, CeliangActivity.class);
                        startActivity(intent);
                    }

                });






}


}